const { AsyncParallelBailHook } = require('tapable');
let hook = new AsyncParallelBailHook(['name', 'age']);
//注册异步任务，然后全部异步任务完成后执行回调 Promise.all
/**
 * tapAsync注册的话，在callback里传值就表示有错误,直接中止
 * tapPromise注册的话，不管调用的是resolve还是reject,只会往这两个方法里传值了，就相当 于有错了，直接中止
 * 
 */
console.time('cost');
hook.tapAsync("1", (name, age, callback) => {
    setTimeout(() => {
        console.log('1', name, age);
        callback();
    }, 1000);
});
let timer3;
hook.tapAsync("2", (name, age, callback) => {
    setTimeout(() => {
        console.log('2', name, age);
        clearTimeout(timer3);
        callback('我是2号有值');
    }, 2000);
});
hook.tapAsync("3", (name, age, callback) => {
    timer3=setTimeout(() => {
        console.log('3', name, age);
        callback();
    }, 3000);
});
hook.callAsync('zhufeng', 12, (err) => {
    console.log('ok');
    console.timeEnd('cost');
});

//AsyncParallelBailHook有一个任务返回值不为空就直接结束 
/* hook.tapPromise('1', function (name, age) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('1', name, age);
            resolve();
        }, 1000);
    });
});
hook.tapPromise('2', function (name, age) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('2', name, age);
            //对于promise来说，resolve还reject并没有区别
            //区别在于你传给它们的参数
            //resolve('我是2号，我有值');
            reject('我是2号，我有值');
        }, 2000);
    });
});
hook.tapPromise('3', function (name, age) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('3', name, age);
            resolve();
        }, 3000);
    });
});
hook.promise('zhufeng',12).then(()=>{
    console.log('success');
    console.timeEnd('cost');
},()=>{
    console.log('fail');
    console.timeEnd('cost');
}); */